package com.ganzalang.gmall.concurrentart.jvm.sync;

import org.openjdk.jol.info.ClassLayout;

/**
 * @author Ganzalang
 * @description Java对象组成
 * @Date 2022/5/11 22:15
 */
public class Layout1 {

    static L l = new L();

    public static void main(String[] args) {
        System.out.println("start");
        System.out.println();

//        System.out.println(Integer.toHexString(l.hashCode()));

        System.out.println(ClassLayout.parseInstance(l).toPrintable());
        /**
         * synchronized锁住的是对象，不是代码
         *
         * 关闭jvm延迟偏向锁的jjvm参数 -XX:BiasedLockingStartupDelay=0
         */
        synchronized (l) {
            System.out.println("locking 1");
            System.out.println(ClassLayout.parseInstance(l).toPrintable());

        }

        synchronized (l) {
            System.out.println("locking 2");
            System.out.println(ClassLayout.parseInstance(l).toPrintable());
        }

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                test1();
            }
        });
        thread1.start();

        System.out.println("end");
    }

    private static void test1() {
        synchronized (l) {
            System.out.println("xx");
            System.out.println(ClassLayout.parseInstance(l).toPrintable());
        }
    }


}
